<template>
	<view class="theabox">
		<view style="text-align: center;" v-if="list.length <= 0" class="intext">--暂无数据--</view>
		<view class="listbox" v-for="(item,index) in list" :key="index">
			<view class="detail">
				<view class="address">
					<u-icon name="map-fill" color="#10c696"></u-icon>
					<view class="text">{{item.place.name}} {{item.place.address}}</view>
				</view>
				<view class="name">
					<view class="img_wrap">
						<image :src="item.place.image" mode=""></image>
					</view>
					<text>{{item.place.name}}</text>
				</view>
				<view class="">订单编号：{{item.order_num}}</view>
				<view class="">下单时间:{{item.start_time_text}}</view>
				<view class="">到期时间:{{item.end_time_text}}</view>
				<view class="">
					数量：x{{item.total_num}}
				</view>
			</view>
			<view class="bottom">
				<view v-if="item.status==1" class="listbutt" @click="gotosy(item)">去使用</view>
				<view v-if="item.status==1" class="listbutt tui" @click="handleRefund(item)">退款</view>
				<view v-else-if="item.status==2" class="listbutt" @click="gotosy(item)">部分销核</view>
				<view v-else-if="item.status==4" class="listbutt1" >已退款</view>
				<view v-else-if="item.status==5" class="listbutt1" >已销核</view>
			</view>
		</view>
		
	</view>
</template>

<script setup>
	import { ref,onMounted } from 'vue';
	import {onReachBottom} from '@dcloudio/uni-app'
	import request from '../../utils/request';
	onMounted(()=>{
		token.value=uni.getStorageSync('token')
		getData()
	})
	let list =ref([])
	let token=ref('')
	let page = ref(1)
	let last_page = ref(1)
	
	onReachBottom(() => {
		if(page.value == last_page.value){
			uni.$u.toast('已经是最后一页了')
		}else{
			page.value++
			getData()
		}
	})
	
	const getData = () => {
		if (page.value == 1) {
			list.value = []
		}
		request.post('Order/lists',{data:{
			type1:'2',
			page: page.value
		},header:{
			"token":token.value
		}}).then(res=>{
			console.log('1323')
			last_page.value = res.data.last_page
			if (res.code == 1) {
				res.data.data.forEach((item,index) => {
					list.value.push(item)
				})
			} else {
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
			}
		}).catch(err=>{
			console.log(err);
		})
	}
	
	const gotosy=(item)=>{
		uni.navigateTo({
			url:'/pages/erwm/erwm?info=' + JSON.stringify(item) + '&type=2'
		})
	}
	
	const handleRefund = (item) => {
		uni.showModal({
			title: '提示',
			content: '是否确认退款？',
			success: function (res) {
				if (res.confirm) {
					uni.requestSubscribeMessage({
						tmplIds: ['ropT_b9f-HPW6onEWhc5q2OTnx62V4aAbM8Xe3kyfP0'],
						success(res) {
							refound(item)
						},
						fail(err) {
							console.log(err, '失败');
							// 处理订阅失败后的逻辑
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	
	const refound = (item) => {
		request.post('Price/refundOrder',{data:{
			order_num:item.order_num
		},header:{
			"token":token.value
		}}).then(res=>{
			uni.$u.toast(res.msg)
			page.value = 1
			setTimeout(() => {
				getData()
			}, 1200)
		}).catch(err=>{
			console.log(err);
		})
	}
</script>

<style scoped lang="scss">
	.theabox{
		width: 100%;
		min-height: 100vh;
		height: fit-content;
		padding: 24rpx;
		background-color: #f2f2f2;
		box-sizing: border-box;
	}     
	.listbox{
		width: 100%;
		height: fit-content;
		background-color: #fff;
		margin-bottom: 32rpx;
		border-radius: 20rpx;
		padding: 24rpx;
		// display: flex;
		// align-items: flex-end;
		// justify-content: space-between;
		box-sizing: border-box;
		
		.name {
			font-size: 30rpx;
			margin-bottom: 20rpx;
		}
		
		.bottom {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			margin-top: 24rpx;
		}
	}
	.listbutt{
		width: 140rpx;
		height: 60rpx;
		background-color: #10c696;
		border-radius: 40rpx;
		line-height: 60rpx;
		text-align:center;
		color: #fff;
	}
	
	.tui {
		background-color: #DE1F28;
		margin-left: 24rpx;
	}
	
	.listbutt1{
		width: 140rpx;
		height: 60rpx;
		background-color: #afafaf;
		border-radius: 40rpx;
		line-height: 60rpx;
		text-align:center;
		color: #fff;
	}
	
	.address {
		display: flex;
		align-items: center;
		padding: 0 0 24rpx;
		border-bottom: 1rpx solid #ddd;
		box-sizing: border-box;
		margin-bottom: 16rpx;
		
		.text {
			width: 90%;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			word-break: break-all;
			margin-left: 16rpx;
		}
	}
	
	.name {
		display: flex;
		align-items: center;
		margin-bottom: 16rpx;
		
		.img_wrap {
			width: 96rpx;
			height: 96rpx;
			margin-right: 24rpx;
			border-radius: 100%;
			
			image {
				width: 100%;
				height: 100%;
				border-radius: 100%;
			}
		}
		
		text {
			font-size: 28rpx;
			font-weight: bold;
		}
	}
</style>
